import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:bot_toast/bot_toast.dart';
import '../data/models/download_record.dart';
import '../modules/download/controllers/download_controller.dart';

/// 全局下载进度指示器
class GlobalDownloadIndicator extends StatelessWidget {
  const GlobalDownloadIndicator({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DownloadController>(
      builder: (downloadController) {
        try {
          // 检查控制器是否可用
          if (!Get.isRegistered<DownloadController>()) {
            return const SizedBox.shrink();
          }

          final allRecords = downloadController.downloadRecords;
          final activeDownloads =
              allRecords
                  .where(
                    (record) =>
                        record.status == DownloadStatus.downloading ||
                        record.status == DownloadStatus.waiting,
                  )
                  .toList();

          if (activeDownloads.isEmpty) {
            return const SizedBox.shrink();
          }

          final totalProgress =
              activeDownloads.fold<double>(
                0.0,
                (sum, record) => sum + record.progress,
              ) /
              activeDownloads.length;

          return Positioned(
            bottom: 300.h,
            right: 20.w,
            child: Material(
              color: Colors.transparent,
              child: GestureDetector(
                onTap: () => Get.toNamed('/download'),
                child: Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 20.w,
                    vertical: 12.h,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.black.withValues(alpha: 0.9),
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(25.r),
                      bottomLeft: Radius.circular(25.r),
                      topRight: Radius.circular(12.r),
                      bottomRight: Radius.circular(12.r),
                    ),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withValues(alpha: 0.3),
                        blurRadius: 10.r,
                        offset: const Offset(0, 2),
                      ),
                    ],
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Icon(Icons.download, color: Colors.white, size: 20.sp),
                      SizedBox(width: 12.w),
                      Text(
                        '下载中 ${(totalProgress * 100).toInt()}%',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 28.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          );
        } catch (e) {
          print('❌ 全局下载进度指示器错误: $e');
          return const SizedBox.shrink();
        }
      },
    );
  }
}
